package net.sxnic.ugr.user.dao;

import java.text.DecimalFormat;
import java.util.Iterator;
import java.util.List;

import net.sxinfo.core.spring25.HibernateDao;
import net.sxnic.ugr.DwType;
import net.sxnic.ugr.group.Group;
import net.sxnic.ugr.role.Role;
import net.sxnic.ugr.user.User;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

@Repository("userDao")
@SuppressWarnings("unchecked")
public class HibernateUserDao extends HibernateDao<User, String> implements
		UserDao {

	private static final String STR_FORMAT = "0000"; 
	
	public int findMaxUserOrder() {
		String sql = "select max(userOrder) from ugr_user";

		Object hanno = getCurrSession().createSQLQuery(sql).uniqueResult();

		if (hanno == null) {
			return 0;
		} else {
			return Integer.parseInt(hanno.toString());
		}
	}

	@Override
	public List<User> getUserInGroup(Group group) {
		Query query = getCurrSession()
				.createQuery(
						"from User as d where ? in elements(d.groups) order by userOrder");
		query.setParameter(0, group);
		return query.list();
	}

	@Override
	public List<User> getUserInRole(Role role) {
		Query query = getCurrSession()
				.createQuery(
						"from User as d where ? in elements(d.roles) order by userOrder");
		query.setParameter(0, role);
		return query.list();
	}

	/**
	 * 根据用户全称和用户类型获取用户实体
	 * 
	 * @param fullname
	 *            用户全称
	 * @param dwType
	 *            枚举类型单位类别
	 * @return 对应的实体
	 */
	public User getUserByFullname(String fullname, DwType dwType) {
		String hql = "from User where fullname=? and zzdwCate = ?";
		return this.findUnique(hql,
				new Object[] { fullname, dwType.getIndex() });
	}

	/**
	 * 获取某种单位类型的用户列表
	 * 
	 * @param dwType
	 *            单位类型
	 * @return 用户列表
	 */
	public List<User> findAllByDwType(DwType dwType) {
		String hql = "from User where zzdwCate=? and enabled=?";
		return this.find(hql, new Object[] { dwType.getIndex(),true });
	}

	/**
	 * 根据单位类别获取用户最大编码
	 * 
	 * @param dwType
	 *            单位类别
	 * @return 指定类别的用户最大编码
	 */
	@SuppressWarnings("rawtypes")
	public String findMaxByDwType(DwType dwType) {
		
		String sql = "select max(username) from ugr_user where zzdwCate='"+dwType.getIndex()+"'";
		
		Object userNo = this.getCurrSession().createSQLQuery(sql).uniqueResult();
		
		String userPrefix = "";
		switch(dwType.getIndex())
		{
			case "001" : userPrefix = "glbm"; break;
			case "002" : userPrefix = "zgbm"; break;
			case "003" : userPrefix = "ytdw";break;
			case "004" : userPrefix = "gczx";break;
		}

		int userMax = 0;	
		if (userNo == null) {
			return userPrefix+"0001";
		}else
		{
			String obj = StringUtils.right((String)userNo, 4);
			userMax = Integer.valueOf(obj);
		}

	    DecimalFormat df = new DecimalFormat(STR_FORMAT);
		return userPrefix+String.valueOf(df.format(userMax+1));
	}
	
    /**
     * 根据用户名前缀获取该类用户的最大编码
     * @param userPrefix 用户名前缀
     * @return 指定前缀的用户最大类别
     */ 
    public String findMaxByUserPrefix(String userPrefix)
    {
		String sql = "select max(username) from ugr_user where username like '"+userPrefix+"%'";
		
		Object userNo = this.getCurrSession().createSQLQuery(sql).uniqueResult();

		int userMax = 0;	
		if (userNo == null) {
			return userPrefix+"0001";
		}else
		{
			String obj = StringUtils.right((String)userNo, 4);
			userMax = Integer.valueOf(obj);
		}

	    DecimalFormat df = new DecimalFormat(STR_FORMAT);
		return userPrefix+String.valueOf(df.format(userMax+1));
    }
}
